import AdItem from "./Items/AdItem";
import MathTool from "../../Tool/MathTool";
import { ui } from "../../../../ui/layaMaxUI";
import { BaseAdView } from "./BaseAdView";
import { AdConfigData } from "./ShiyouAdManager";

export default class MainAdViewUI extends ui.ShiyouAdView.MainAdViewUI implements BaseAdView {


    private m_leftAdItem: AdItem = null;
    private m_rihghtAdItem: AdItem = null;
    private m_addataarray:Array<AdConfigData> = [];

    constructor()  {
        super();
        Laya.stage.addChild(this);
        this.m_leftAdItem = this.m_leftAd as AdItem;
        this.m_rihghtAdItem = this.m_rightAd as AdItem;
        this.m_leftAdItem.ShowRotate();
        this.m_rihghtAdItem.ShowRotate();
    }

    InitData(gameadsData: Array<AdConfigData>) {

        if (gameadsData && gameadsData.length < 1) {
            return;
        }
 
        if (this.m_addataarray.length<1) {
            for (let index = 0; index < gameadsData.length; index++) {
                const element = gameadsData[index];
                if (element.Type2 !=null) {
                    this.m_addataarray.push(element);
                }
            }
        }
        if (this.m_addataarray.length < 2) {
            return;
        }
        let indexs = MathTool.GetRandomTwoNumInt(0, this.m_addataarray.length);
        let leftdata=this.m_addataarray[indexs[0]];
        let rightdata=this.m_addataarray[indexs[1]];
        this.m_leftAdItem.SetData(leftdata,"Type2",leftdata.Type2);
        this.m_rihghtAdItem.SetData(rightdata,"Type2",rightdata.Type2);
    }



    Show(top:number) {
        this.visible=true;
        Laya.timer.loop(10000, this, this.InitData, [this.m_addataarray]);
        if (top) {
            this.m_leftAd.top=top;
            this.m_rightAd.top=top;  
        }
      
    }

    Hide() {
        Laya.timer.clearAll(this);
        this.visible = false;
    }
}